﻿using FarPoint.Win.Spread;
using GrapeCity.Spreadsheet;
using Spread.Common.Features.Sample;
using System;

namespace Spread.Worksheet.Features
{
    public partial class CenterAcrossSelection : BaseSampleControl
    {
        public CenterAcrossSelection()
        {
            InitializeComponent();
            Name = "Center Across Selection";
            SetupSheet(_fpSpread);
        }

        private void SetupSheet(FpSpread spread)
        {
            var workbook = spread.AsWorkbook();
            spread.Features.ExcelCompatibleKeyboardShortcuts = true;

            // Enable center across selection
            spread.AllowCellOverflow = true;
            spread.BorderCollapse = BorderCollapse.Enhanced;

            IStyle cas = workbook.Styles.Add("cas");
            cas.HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.CenterAcrossSelection;

            var borderStyle = GrapeCity.Spreadsheet.BorderLineStyle.Thin;
            var borderColor = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Black);
            var border = new GrapeCity.Spreadsheet.Border(new GrapeCity.Spreadsheet.BorderLine(borderStyle, borderColor));
            cas.Borders.ApplyBorder(border);

            var workSheet = workbook.ActiveSheet;
            workSheet.Name = "Center Across Selection";

            int column = 1;
            foreach (var weekDay in Enum.GetNames(typeof(DayOfWeek)))
            {
                workSheet.Cells[1, column].Value = weekDay;
                workSheet.Columns[column].ColumnWidth = 150;
                column++;
            }

            for (int hour = 12; hour <= 16; hour++)
            {
                string cell = $"A{hour - 9}";
                workSheet.Cells[cell].Value = $"{hour}:00";
            }

            workSheet.Columns["A"].ColumnWidth = 100;

            workSheet.Cells["A2:H7"].Borders.ApplyBorder(border);
            workSheet.Cells["B3"].Value = "Walker";
            workSheet.Cells["B4:B5"].Value = "Today News";
            workSheet.Cells["B6:B7"].Value = "News";
            workSheet.Cells["C3"].Value = "Morning Show";
            workSheet.Cells["D4"].Value = "Sesame Street";
            workSheet.Cells["D5"].Value = "Kids Zone";
            workSheet.Cells["E3"].Value = "Sport";
            workSheet.Cells["E4:E5"].Value = "Football";
            workSheet.Cells["F3"].Value = "Weather";
            workSheet.Cells["F4"].Value = "Market Watch";
            workSheet.Cells["F4"].Value = "Soap Opera";
            workSheet.Cells["G3:G7"].Value = "N/A";
            workSheet.Cells["A2:H7"].Select();
        }

        private void OnCenterAcrossSelectionClick(object sender, EventArgs e)
        {
            IWorksheet workbook = _fpSpread.AsWorkbook().ActiveSheet;
            IRange selection = workbook.Selection;
            workbook.Cells[selection.Row, selection.Column, selection.Row2, selection.Column2].ApplyStyle("cas");
        }
    }
}




//
// CenterAcrossSelection.Designer.cs
//

namespace Spread.Worksheet.Features
{
    partial class CenterAcrossSelection
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CenterAcrossSelection));
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.centerAcrossSelectionToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this._fpSpread = new FarPoint.Win.Spread.FpSpread(FarPoint.Win.Spread.LegacyBehaviors.None, ((object)(resources.GetObject("pnlMain.Controls"))));
            this._fpSpread_Sheet1 = this._fpSpread.GetSheet(0);
            this.pnlMain.SuspendLayout();
            this.menuStrip1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this._fpSpread)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this._fpSpread);
            this.pnlMain.Controls.Add(this.menuStrip1);
            // 
            // pnlControls
            // 
            this.pnlControls.Size = new System.Drawing.Size(210, 599);
            // 
            // menuStrip1
            // 
            this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.centerAcrossSelectionToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(636, 28);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // centerAcrossSelectionToolStripMenuItem
            // 
            this.centerAcrossSelectionToolStripMenuItem.Name = "centerAcrossSelectionToolStripMenuItem";
            this.centerAcrossSelectionToolStripMenuItem.Size = new System.Drawing.Size(178, 24);
            this.centerAcrossSelectionToolStripMenuItem.Text = "Center Across Selection";
            this.centerAcrossSelectionToolStripMenuItem.Click += new System.EventHandler(this.OnCenterAcrossSelectionClick);
            // 
            // _fpSpread
            // 
            this._fpSpread.AccessibleDescription = "Book1, Sheet1, Row 0, Column 0";
            this._fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this._fpSpread.Font = new System.Drawing.Font("Calibri", 11F);
            this._fpSpread.Location = new System.Drawing.Point(0, 28);
            this._fpSpread.Name = "_fpSpread";
            this._fpSpread.Size = new System.Drawing.Size(636, 571);
            this._fpSpread.TabIndex = 1;
            // 
            // CenterAcrossSelection
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "CenterAcrossSelection";
            this.pnlMain.ResumeLayout(false);
            this.pnlMain.PerformLayout();
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this._fpSpread)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem centerAcrossSelectionToolStripMenuItem;
        private FarPoint.Win.Spread.FpSpread _fpSpread;
        private FarPoint.Win.Spread.SheetView _fpSpread_Sheet1;
    }
}